package com.htg.util;

public class UtilOsmdroid {

    /**
     *
     * @param x
     * @param y
     * @param zoom
     * @return 北东南西
     */
    public static double[] tile2boundingBox(final int x, final int y, final int zoom) {
        return new double[]{tile2lat(y, zoom), tile2lon(x + 1, zoom), tile2lat(y + 1, zoom), tile2lon(x, zoom)};
    }

    public static double tile2lon(int x, int z) {
        return x / Math.pow(2.0, z) * 360.0 - 180;
    }

    public static double tile2lat(int y, int z) {
        double n = Math.PI - (2.0 * Math.PI * y) / Math.pow(2.0, z);
        return Math.toDegrees(Math.atan(Math.sinh(n)));
    }


    public static double[] tileXYToLonLat(int x, int y, int zoom) {

        double[] lonlat = new double[2];

        lonlat[0] = tile2lon(x, zoom);
        lonlat[1] = tile2lat(y, zoom);

        return lonlat;
    }

    public static int[] lonLatToTileXY(double lon, double lat, int zoom) {
        int[] tileXY = new int[2];
        double n = Math.pow(2.0, zoom);
        tileXY[0] = (int)((lon + 180.0) / 360.0 * n);
        tileXY[1] = (int)((1.0 - Math.log(Math.tan(Math.toRadians(lat)) + 1.0 / Math.cos(Math.toRadians(lat))) / Math.PI) / 2.0 * n);
        return tileXY;
    }
}
